'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en', 'datetimepicker', 'koala-search', 'modalSearch'], function ($) {
  return {
    init: function () {
      this.initPage();
      this.initSearch();
      this.initUpload();
      this.initDelete();
      this.initAudit();
      this.initBatchAudit();
      this.initDimission();
    },

    initPage: function () {
      
      $("#driver-upload-result").modal('show');
      
      $('a.batch-submit').addClass('disabled');

      $(document).on('click', '#checkAll', function(e) {
        $('input[name="id"]').prop('checked', $('#checkAll').is(':checked'));
      });
      
      $(document).on('change', '#checkAll, input[name="id"]', function(e) {
        var ids = $('input[name="id"]:checked').val();
        if (ids) {
          $('a.batch-submit').removeClass('disabled');
        } else {
          $('a.batch-submit').addClass('disabled');
        }
      });
      
    },
    
    initSearch: function() {
      
      $(document).on('shown.bs.modal', '#driver-search', function(e) {
        
        $('[data-target="#modal-search-vehicle"]').modalsearch({
          url: function(){
                var url = window.__CTX__.root + '/vehicles/query';
                return url;
             },
            tableColumns: ['驳运车代码', '驳运车名称'],
            tableValues: ['code', 'name'],
            displayValues: ['code', 'name'],
            values: ['id']
          });
        
        var options = {
          time: 1000,
          url: window.__CTX__.root + "/vehicles/query",
          queryName: "code",
          value: ["id"],
          display: ["code", "name"]
        }
        
        $('.vehicle-search').koalaSearch(options);
        
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        
      });
      
    },
    
    initUpload: function() {
      
      $(document).on('shown.bs.modal', '#driver-upload', function(e) {
        
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        
        document.getElementById("cover").onchange = function () {
          document.getElementById("coverFile").value = this.value;
        }
        
      })
      
    },
    
    initDelete: function() {
      
      $(document).on('shown.bs.modal', '#driver-delete', function(e) {
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
      });
      
    },
    
    initAudit: function() {
        
      $(document).on('shown.bs.modal', '#driver-audit', function(e) {
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
      });
        
    },
    
    initBatchAudit: function() {
      
      $(document).on('shown.bs.modal', '#driver-batch-audit', function(e) {
        
        var idArray = new Array();
        $('input[name="id"]:checked').each(function() {
          var id = $(this).val();
          idArray.push(id);
        });
        $('input[name="driverIds"]').val(idArray);
        
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
        
      });
      
    },
      
    initDimission: function() {
          
      $(document).on('shown.bs.modal', '#dimission', function(e) {
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
      });
          
    }

  }
})